/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package stis.statpro.ibs.view;

import com.toedter.calendar.JDateChooser;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JTextField;
import stis.statpro.ibs.dao.KabupatenDao;
import stis.statpro.ibs.dao.KecamatanDao;
import stis.statpro.ibs.dao.ProvinsiDao;
import stis.statpro.ibs.datasource.Datasource;
import stis.statpro.ibs.model.Kabupaten;
import stis.statpro.ibs.model.Kecamatan;
import stis.statpro.ibs.model.Provinsi;
import stis.statpro.ibs.widget.WidgetPanelInsertA;

/**
 *
 * @author habib <imamhabibpamungkas@gmail.com>
 */
public class PanelInsertA extends javax.swing.JPanel {

    private List<String> listKab;
    private List<String> listNamaProp;
    private int indProp = -1;
    private int indKab = -1;
    private int indKec = -1;

    /**
     * Creates new form PanelInsertA
     */
    public PanelInsertA() {
        initComponents();
        try {
            initDataProp(indProp);
            initDataKab(indKab);
            initDataKec(indKec);
        } catch (SQLException ex) {
            Logger.getLogger(PanelInsertA.class.getName()).log(Level.SEVERE, null, ex);
        }
        propinsi.addItemListener(new ComboBoxPropListener());
        kabupaten.addItemListener(new ComboBoxKabListener());
    }

    public JDateChooser getTanggalInput() {
        return tanggalInput;
    }

    public JLabel getLabelTanggal() {
        return labelTanggal;
    }

    public void addListenerNext(ActionListener l) {
        buttonNext.addActionListener(l);
    }

    public void setIndProp(int ind) {
        this.indProp = ind;
    }

    public void setIndKab(int indKab) {
        this.indKab = indKab;
    }

    public void setIndKec(int indKec) {
        this.indKec = indKec;
    }

    public JTextField getDesaKelurahan() {
        return desaKelurahan;
    }

    public JComboBox getKabupaten() {
        return kabupaten;
    }

    public JComboBox getKecamatan() {
        return kecamatan;
    }

    public JTextField getKip() {
        return kip;
    }

    public JTextField getKotaAdministratif() {
        return kotaAdministratif;
    }

    public JComboBox getPropinsi() {
        return propinsi;
    }

    public WidgetPanelInsertA getWidgetPanelInsertA1() {
        return widgetPanelInsertA1;
    }

    public JButton getButtonNext() {
        return buttonNext;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        widgetPanelInsertA1 = new stis.statpro.ibs.widget.WidgetPanelInsertA();
        propinsi = new javax.swing.JComboBox();
        kabupaten = new javax.swing.JComboBox();
        kotaAdministratif = new javax.swing.JTextField();
        kecamatan = new javax.swing.JComboBox();
        desaKelurahan = new javax.swing.JTextField();
        kip = new javax.swing.JTextField();
        buttonNext = new javax.swing.JButton();
        tanggalInput = new com.toedter.calendar.JDateChooser();
        labelTanggal = new javax.swing.JLabel();

        setMaximumSize(new java.awt.Dimension(640, 450));
        setMinimumSize(new java.awt.Dimension(640, 450));
        setPreferredSize(new java.awt.Dimension(640, 450));
        setLayout(new java.awt.BorderLayout());

        kecamatan.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "kecamatan 1", "kecamatan 2" }));

        buttonNext.setText("Next");

        tanggalInput.setDateFormatString("yyyy-MM-dd");

        labelTanggal.setText("Tanggal Input");

        javax.swing.GroupLayout widgetPanelInsertA1Layout = new javax.swing.GroupLayout(widgetPanelInsertA1);
        widgetPanelInsertA1.setLayout(widgetPanelInsertA1Layout);
        widgetPanelInsertA1Layout.setHorizontalGroup(
            widgetPanelInsertA1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(widgetPanelInsertA1Layout.createSequentialGroup()
                .addContainerGap(397, Short.MAX_VALUE)
                .addGroup(widgetPanelInsertA1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, widgetPanelInsertA1Layout.createSequentialGroup()
                        .addGroup(widgetPanelInsertA1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(desaKelurahan, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(kecamatan, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(propinsi, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(kabupaten, javax.swing.GroupLayout.Alignment.TRAILING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(kotaAdministratif, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(77, 77, 77))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, widgetPanelInsertA1Layout.createSequentialGroup()
                        .addComponent(buttonNext)
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, widgetPanelInsertA1Layout.createSequentialGroup()
                        .addComponent(labelTanggal)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(widgetPanelInsertA1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(kip, javax.swing.GroupLayout.DEFAULT_SIZE, 130, Short.MAX_VALUE)
                            .addComponent(tanggalInput, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(49, 49, 49))))
        );
        widgetPanelInsertA1Layout.setVerticalGroup(
            widgetPanelInsertA1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(widgetPanelInsertA1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(kip, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(widgetPanelInsertA1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tanggalInput, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelTanggal))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 134, Short.MAX_VALUE)
                .addComponent(propinsi, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(kabupaten, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(10, 10, 10)
                .addComponent(kotaAdministratif, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(9, 9, 9)
                .addComponent(kecamatan, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(desaKelurahan, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(buttonNext)
                .addContainerGap())
        );

        add(widgetPanelInsertA1, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonNext;
    private javax.swing.JTextField desaKelurahan;
    private javax.swing.JComboBox kabupaten;
    private javax.swing.JComboBox kecamatan;
    private javax.swing.JTextField kip;
    private javax.swing.JTextField kotaAdministratif;
    private javax.swing.JLabel labelTanggal;
    private javax.swing.JComboBox propinsi;
    private com.toedter.calendar.JDateChooser tanggalInput;
    private stis.statpro.ibs.widget.WidgetPanelInsertA widgetPanelInsertA1;
    // End of variables declaration//GEN-END:variables

    public void initDataProp(int x) throws SQLException {
        ProvinsiDao p = new ProvinsiDao();
        for (Provinsi prov : p.getALl()) {
            propinsi.addItem(prov);
        }
        if (x > -1) {
            propinsi.setSelectedIndex(x);
        }
    }

    public void initDataKab(int x) {
        Provinsi p = (Provinsi) propinsi.getSelectedItem();
        KabupatenDao kab = new KabupatenDao();
        try {
            for (Kabupaten k : kab.getALl(p.getId())) {
                if (k.getIdProv() == p.getId()) {
                    kabupaten.addItem(k);
                }
            }
            if (x > -1) {
                kabupaten.setSelectedIndex(x);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelInsertA.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void initDataKec(int x) {
        kecamatan.removeAllItems();
        KecamatanDao kec = new KecamatanDao();
        try {
            for (String s : kec.getALl(((Kabupaten) kabupaten.getSelectedItem()).getIdKab())) {
                kecamatan.addItem(s);
            }
            if (x > -1) {
                kecamatan.setSelectedIndex(x);
            }
        } catch (SQLException ex) {
            Logger.getLogger(PanelInsertA.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    class ComboBoxKabListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            kecamatan.removeAllItems();
            KecamatanDao kec = new KecamatanDao();
            try {
                if (kabupaten.getSelectedItem() != null) {
                    for (String s : kec.getALl(((Kabupaten) kabupaten.getSelectedItem()).getIdKab())) {
                        kecamatan.addItem(s);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(PanelInsertA.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

    }

    class ComboBoxPropListener implements ItemListener {

        @Override
        public void itemStateChanged(ItemEvent e) {
            kabupaten.removeAllItems();
            try {
                Provinsi p = (Provinsi) propinsi.getSelectedItem();
                KabupatenDao kab = new KabupatenDao();
                for (Kabupaten k : kab.getALl(p.getId())) {
                    if (k.getIdProv() == p.getId()) {
                        kabupaten.addItem(k);
                    }
                }
            } catch (SQLException ex) {
                Logger.getLogger(PanelInsertA.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
